package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.constant.JobNatureEnum;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.EnumUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ArrangeTeacherHistory;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.mapper.ArrangeTeacherHistoryMapper;
import com.yuyou.fn.educational.service.IArrangeTeacherHistoryService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.ArrangeTeacherHistoryDetailVo;
import com.yuyou.fn.educational.vo.ArrangeTeacherHistoryQueryVo;
import com.yuyou.fn.platform.permission.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * ArrangeTeacherHistory 表数据服务层接口实现类
 *
 */
@Service
public class ArrangeTeacherHistoryServiceImpl extends SuperServiceImpl<ArrangeTeacherHistoryMapper, ArrangeTeacherHistory,Long> implements IArrangeTeacherHistoryService {

    @Resource
    private ITeacherService teacherService;


    @Override
    public Page<ArrangeTeacherHistoryDetailVo> findArrageTeacherHistoryPage(DataPermission dataPermission,ArrangeTeacherHistoryQueryVo arrangeTeacherHistoryQueryVo, int pageNo, int pageSize) {
        if (arrangeTeacherHistoryQueryVo.getAreaTeamId()==null){
            throw new BusException("区域id必传");
        }
        if (arrangeTeacherHistoryQueryVo.getBusTeamIds()==null){
            arrangeTeacherHistoryQueryVo.setBusTeamIds(dataPermission.getBusTeamIds());
        }
        Page<ArrangeTeacherHistoryDetailVo> page=new Page<>(pageNo,pageSize);
        if (arrangeTeacherHistoryQueryVo.getBusTeamId()==null && !Utils.checkListNotEmpty(arrangeTeacherHistoryQueryVo.getBusTeamIds())){
            page.setTotal(0);
            return page;
        }
        if (pageNo!=0){
            pageNo=pageNo-1;
        }
        Integer toal=this.countArrageTeacherHistory(arrangeTeacherHistoryQueryVo);
        List<ArrangeTeacherHistoryDetailVo> arrangeTeacherHistoryDetailVoList=this.findLimitList(arrangeTeacherHistoryQueryVo,pageNo*pageSize,pageSize);

        page.setTotal(toal);
        page.setRecords(arrangeTeacherHistoryDetailVoList);
        return page;
    }

    @Override
    public SXSSFWorkbook exportArrageTeacherHistory(DataPermission dataPermission,ArrangeTeacherHistoryQueryVo arrangeTeacherHistoryQueryVo) {
        if (arrangeTeacherHistoryQueryVo.getAreaTeamId()==null){
            throw new BusException("区域id必传");
        }
        if (arrangeTeacherHistoryQueryVo.getBusTeamIds()==null){
            arrangeTeacherHistoryQueryVo.setBusTeamIds(dataPermission.getBusTeamIds());
        }
        List<ArrangeTeacherHistoryDetailVo> arrangeTeacherHistoryDetailVoList = this.findLimitList(arrangeTeacherHistoryQueryVo,0,50000);
        return arrageTeacherHistory2Excel(arrangeTeacherHistoryDetailVoList);
    }

    public static SXSSFWorkbook arrageTeacherHistory2Excel(List<ArrangeTeacherHistoryDetailVo> arrangeTeacherHistoryDetailVoList) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("教师临调情况");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{"期数","业务组","年级","学科","班级名称","班级编号","调整讲次 ","上课日期","任教老师(原)","任教性质(原)","任教老师(现)","任教性质(现)","临调原因","修改人","修改时间"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum=1;
        for (ArrangeTeacherHistoryDetailVo arrangeTeacherHistoryDetailVo:arrangeTeacherHistoryDetailVoList){
            int cellNum = 0;
            SXSSFRow row = sheet.createRow(rowNum++);

            SXSSFCell cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getPeriodName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getBusTeamName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getGradeName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getSubjectName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getClassName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getClassNo()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(arrangeTeacherHistoryDetailVo.getLectureNo());

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getClassDate()));


            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getOrginTeacherName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(EnumUtils.getName(JobNatureEnum.values() ,arrangeTeacherHistoryDetailVo.getOrginJobNature()));



  /*          cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getOrginIdNo()));
*/
            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getNowTeacherName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(EnumUtils.getName(JobNatureEnum.values() ,arrangeTeacherHistoryDetailVo.getNowJobNature()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getRemark()));


      /*      cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getNowIdNo()));
*/
            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(arrangeTeacherHistoryDetailVo.getOperatorName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(Utils.formatDate(arrangeTeacherHistoryDetailVo.getChangeTime(),"yyyy-MM-dd HH:mm:ss"));

        }
        return excel;
    }



    @Override
    public List<ArrangeTeacherHistoryDetailVo> findLimitList(ArrangeTeacherHistoryQueryVo arrangeTeacherHistoryQueryVo, int start, int end) {
        List<ArrangeTeacherHistoryDetailVo> arrangeTeacherHistoryDetailVoList=autoMapper.findArrageTeacherHistoryLimitList(arrangeTeacherHistoryQueryVo,start,end);
        List<Long> orginTeacherIds=BeanUtils.toList(arrangeTeacherHistoryDetailVoList,"orginTeacherId");
        List<Long> nowTeacherIds=BeanUtils.toList(arrangeTeacherHistoryDetailVoList,"nowTeacherId");
        List<Long> teacherIds=new ArrayList<>();
        teacherIds.addAll(orginTeacherIds);
        teacherIds.addAll(nowTeacherIds);
        teacherIds= Utils.removeDuplicate(teacherIds);
        List<Teacher> teacherList=teacherService.findByIds(teacherIds);
        Map<Long,Teacher> teacherMap=BeanUtils.toMap(teacherList,"teacherId");
        for (ArrangeTeacherHistoryDetailVo arrangeTeacherHistoryDetailVo:arrangeTeacherHistoryDetailVoList){
            Teacher orginTeacher=teacherMap.get(arrangeTeacherHistoryDetailVo.getOrginTeacherId());
            Teacher nowTeacher=teacherMap.get(arrangeTeacherHistoryDetailVo.getNowTeacherId());
            arrangeTeacherHistoryDetailVo.setOrginIdNo(orginTeacher.getIdNo());
            arrangeTeacherHistoryDetailVo.setOrginJobNature(orginTeacher.getJobNature().intValue());
            arrangeTeacherHistoryDetailVo.setOrginTeacherName(orginTeacher.getTeacherName());
            arrangeTeacherHistoryDetailVo.setNowIdNo(nowTeacher.getIdNo());
            arrangeTeacherHistoryDetailVo.setNowJobNature(nowTeacher.getJobNature().intValue());
            arrangeTeacherHistoryDetailVo.setNowTeacherName(nowTeacher.getTeacherName());
        }
        return arrangeTeacherHistoryDetailVoList;
    }

    @Override
    public Integer countArrageTeacherHistory(ArrangeTeacherHistoryQueryVo arrangeTeacherHistoryQueryVo) {
        return autoMapper.countArrageTeacherHistory(arrangeTeacherHistoryQueryVo);
    }
}